<%@ page pageEncoding="utf-8"%>
<%@ page import="
com.alibaba.fastjson.TypeReference,org.apache.http.HttpResponse,org.apache.http.client.methods.HttpPost,org.apache.http.entity.StringEntity,org.apache.http.impl.client.DefaultHttpClient"%>
<%@ page import="org.apache.http.util.EntityUtils" %>
<%@ page import="org.apache.http.util.EntityUtils" %>
<%!
	// 操作用户相关
	
	// 环信用户注册带有验证机制
	public Message jsonPostWithHeader(String url, Map jsonParam, String header) {
		Message msg = new Message();
		String resData = null;
		try {
			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpPost method = new HttpPost(url);
			StringEntity entity = new StringEntity(JSONUtil.toJson(jsonParam),
					"utf-8");
			entity.setContentEncoding("UTF-8");
			entity.setContentType("application/json");
			method.setEntity(entity);
			method.addHeader("Authorization", "Bearer " + header);
			HttpResponse result = httpClient.execute(method);
			int code = result.getStatusLine().getStatusCode();
			// 根据环信开发者文档，状态码400说明用户名密码有问题
			if (400 == code) {
				return new Message(400, "用户名已存在");
			} else {
				// 请求结束，返回结果
				resData = EntityUtils.toString(result.getEntity());
				Map map = JSONUtil.fromJson(resData, new TypeReference<Map>(){});
				msg.put("user", map);
				return msg;
			}
		} catch (Exception e) {
			return new Message(500, "服务器繁忙");
		}
	}
	
	// 注册
	public Message register(Input input) {
		// input 中传递参数 username password, application
		String application = input.getDesc("application").replace("#", "/");
		String USER_REGISTER = "https://a1.easemob.com/" + application + "/user";
		String server = input.getRequest().getHeader("host");
		Map param = input.getParameters();
		param.put("type", "2");
		String json = HttpUtil.encryptPost("http://" + server + "/v3/access_token/get", param);
		Map resMap =  JSONUtil.fromJson(json, new TypeReference<Map>(){});
		Integer errcode = (Integer)resMap.get("errcode");
		String token = "";
		if (errcode == 20004 || errcode == 20002) {
			json = HttpUtil.encryptPost("http://" + server + "/v3/access_token/create", param);
			resMap =  JSONUtil.fromJson(json, new TypeReference<Map>(){});
			token = Tools.getDesc(resMap.get("access_token"));
		} else {
			token = Tools.getDesc(resMap.get("access_token"));
		}
		return jsonPostWithHeader(USER_REGISTER, param, token);
	}
	
	// 注册
	public Message register(String userName, String password, String application, Integer hospital) {
		Map param = new HashMap();
		param.put("username", userName);
		param.put("password", password);
		param.put("application", application);
		param.put("hospital", hospital);
		String USER_REGISTER = "https://a1.easemob.com/" + application + "/user";
		param.put("type", "2");
		String json = HttpUtil.encryptPost("http://api.tianxiabuyi.com:18080/v3/access_token/get", param);
		Map resMap =  JSONUtil.fromJson(json, new TypeReference<Map>(){});
		Integer errcode = (Integer)resMap.get("errcode");
		String token = "";
		if (errcode == 20004 || errcode == 20002) {
			json = HttpUtil.encryptPost("http://api.tianxiabuyi.com:18080/v3/access_token/create", param);
			resMap =  JSONUtil.fromJson(json, new TypeReference<Map>(){});
			token = Tools.getDesc(resMap.get("access_token"));
		} else {
			token = Tools.getDesc(resMap.get("access_token"));
		}
		return jsonPostWithHeader(USER_REGISTER, param, token);
	}
%>
